﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Data.Filtering;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;

public class DateTemplate : ITemplate
{
    // client IDs of two ASPxDateEdit controls
    public String cIdFrom;
    public String cIdTo;
    public String FieldName;
    public ASPxGridView Container;

    public DateTemplate(ASPxGridView container, string fieldname)
        : base()
    {
        Container = container;
        FieldName = fieldname;
    }

    public void InstantiateIn(Control container)
    {
        Table table = new Table();
        container.Controls.Add(table);
        TableRow row = new TableRow();
        table.Rows.Add(row);

        TableCell cell = new TableCell();
        row.Cells.Add(cell);
        ASPxLabel lbl = new ASPxLabel();
        lbl.ID = "lblFrom";
        lbl.Text = "从：";
        cell.Controls.Add(lbl);

        cell = new TableCell();
        row.Cells.Add(cell);
        ASPxDateEdit dateFrom = new ASPxDateEdit();
        dateFrom.ID = "dateFrom";
        dateFrom.EnableClientSideAPI = true;
        cell.Controls.Add(dateFrom);

        row = new TableRow();

        table.Rows.Add(row);

        cell = new TableCell();
        row.Cells.Add(cell);
        lbl = new ASPxLabel();
        lbl.ID = "lblTo";
        lbl.Text = "到：";
        cell.Controls.Add(lbl);

        cell = new TableCell();
        row.Cells.Add(cell);
        ASPxDateEdit dateTo = new ASPxDateEdit();
        dateTo.ID = "dateTo";
        dateTo.EnableClientSideAPI = true;
        cell.Controls.Add(dateTo);

        cIdFrom = dateFrom.ClientID;
        cIdTo = dateTo.ClientID;

        row = new TableRow();

        table.Rows.Add(row);

        cell = new TableCell();
        cell.ColumnSpan = 2;
        row.Cells.Add(cell);
        ASPxHyperLink lnk = new ASPxHyperLink();
        lnk.ID = "lnkApply";
        lnk.Text = "应用";
        lnk.NavigateUrl = "#";
        lnk.ClientSideEvents.Click =
String.Format("function (s, e) {{ {0}.HideDropDown(); ApplyFilter({0}, '{1}', {2}, {3}, {4}); }}",
            container.NamingContainer.NamingContainer.ClientID, FieldName, cIdFrom, cIdTo, Container.ClientID);
        cell.Controls.Add(lnk);

        Literal space = new Literal();
        space.Text = "&nbsp;&nbsp;";
        cell.Controls.Add(space);

        ASPxHyperLink lnkCancel = new ASPxHyperLink();
        lnkCancel.ID = "lnkCancel";
        lnkCancel.Text = "清除";
        lnkCancel.NavigateUrl = "#";
        lnkCancel.ClientSideEvents.Click =
String.Format("function (s, e) {{ {0}.HideDropDown(); RemoveFilter({0}, '{1}', {2}); }}",
            container.NamingContainer.NamingContainer.ClientID, FieldName, Container.ClientID);
        cell.Controls.Add(lnkCancel);
    }
}


public class DateRangeHandler
{
    DateTemplate dateTemplate = null;
    List<string> FieldNames = null;

    public DateRangeHandler(ASPxGridView sender, string fieldname)
    {
        FieldNames = new List<string>();
        FieldNames.Add(fieldname);
        sender.AutoFilterCellEditorCreate += grid_AutoFilterCellEditorCreate;
        sender.AutoFilterCellEditorInitialize += grid_AutoFilterCellEditorInitialize;
        sender.ProcessColumnAutoFilter += grid_ProcessColumnAutoFilter;
    }

    public DateRangeHandler(ASPxGridView sender, List<string> fieldnames)
    {
        FieldNames = fieldnames;
        sender.AutoFilterCellEditorCreate += grid_AutoFilterCellEditorCreate;
        sender.AutoFilterCellEditorInitialize += grid_AutoFilterCellEditorInitialize;
        sender.ProcessColumnAutoFilter += grid_ProcessColumnAutoFilter;
    }

    private void grid_AutoFilterCellEditorCreate(object sender, ASPxGridViewEditorCreateEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;
        if (FieldNames.Contains(e.Column.FieldName))
        {
            DropDownEditProperties dde = new DropDownEditProperties();
            dde.EnableClientSideAPI = true;

            dateTemplate = new DateTemplate(sender as ASPxGridView, e.Column.FieldName);

            dde.DropDownWindowTemplate = dateTemplate;
            dde.Style.BorderLeft.BorderStyle = BorderStyle.Solid;
            dde.Style.BorderRight.BorderStyle = BorderStyle.Solid;
            dde.Style.BorderTop.BorderStyle = BorderStyle.Solid;
            dde.Style.BorderBottom.BorderStyle = BorderStyle.Solid;
            e.EditorProperties = dde;
        }
    }

    private void grid_AutoFilterCellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;
        if (FieldNames.Contains(e.Column.FieldName))
        {
            ASPxDropDownEdit dde = e.Editor as ASPxDropDownEdit;
            //dde.ClientSideEvents.CloseUp = String.Format("function (s, e) {{ ApplyFilter(s,'{0}', {1}, {2}, {3}); }}",
            //FieldName, dateTemplate.cIdFrom, dateTemplate.cIdTo, (sender as ASPxGridView).ClientID);
            dde.ClientSideEvents.DropDown = String.Format("function (s, e) {{ OnDropDown(s, {0}, {1}, {2}); }}",
                dateTemplate.cIdFrom, dateTemplate.cIdTo, (sender as ASPxGridView).ClientID);
            dde.ReadOnly = true;
        }
    }

    private void grid_ProcessColumnAutoFilter(object sender, ASPxGridViewAutoFilterEventArgs e)
    {
        if (e.Value == "")
            return;
        if (FieldNames.Contains(e.Column.FieldName))
        {
            if (e.Kind == GridViewAutoFilterEventKind.CreateCriteria)
            {
                HttpContext.Current.Session["CollectionDateFilter_" + e.Column.FieldName] = e.Value;
                String[] dates = e.Value.Split(new string[] { " 至 " }, StringSplitOptions.RemoveEmptyEntries);
                DateTime dateFrom = Convert.ToDateTime(dates[0] + " 00:00:00"),
                    dateTo = Convert.ToDateTime(dates[1] + " 23:59:59");
                e.Criteria = (new OperandProperty(e.Column.FieldName) >= dateFrom) &
                             (new OperandProperty(e.Column.FieldName) <= dateTo);
            }
            else
            {
                if (HttpContext.Current.Session["CollectionDateFilter_" + e.Column.FieldName] != null)
                    e.Value = HttpContext.Current.Session["CollectionDateFilter_" + e.Column.FieldName].ToString();
            }
        }
        else
        {
            if (e.Kind == GridViewAutoFilterEventKind.CreateCriteria)
            {
                long dte = DateTime.Now.Ticks;
                e.Criteria = e.Criteria | (new OperandProperty(e.Column.FieldName) == dte);
            }
        }
    }
}